<?php

/**
 * entries actions.
 *
 * @package    berta-s
 * @subpackage entries
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class entryActions extends sfActions {
	/**
	 *
	 * @param sfWebRequest $request
	 * @return Entry|boolean
	 *
	 */
	private function initEntry(){
		$entry = $this->getRoute()->getObject();
		$this->forward404Unless($entry, "Object doesnt exist");

		return $entry;
	}

	/* @TODO: DRY up. duplicate with section! */
	/* @TODO: remove incoming data container ("entry") */
	private function saveEntryFromRequest(sfWebRequest $request){
		if ($request->hasParameter("entry")) {
			$form=new EntryFormForBackend($this->entry);
			$inputParams=$request->getParameter("entry");
			// have no idea, why we need this, Entry should be initialized from form constructor
			if (!empty($this->entry)){
				$inputParams["id"]=$this->entry->getId();
			}

			$useFields=array();
			foreach ($inputParams as $name => $param){
				$useFields[]=$name;
			}
			if (sizeof($useFields)>0){
				$form->useFields($useFields);
			}

			$form->bind($inputParams);
			if ($form->isValid()) {
				$form->save();
				$values=array();
				$values["id"]=$form->getObject()->getId();
				foreach ($inputParams as $key => $value){
					if ($key!="id" && $key!="_csrf_token"){
						$values[$key]=$form->getValue($key);
					}
				}
				$this->values=$values;

				$this->setTemplate("XMLHttp", "global");
			}else{
				$this->setTemplate("XMLHttpError", "global");
			}
			$this->form=$form;
		}else{
			$this->setTemplate("XMLHttp");
		}
	}

//	public function preExecute(){
//		$request=$this->getRequest();
//		if ($request->isXmlHttpRequest()){
//			$request->setParameter("sf_format", "json");
//		}
//	}

	public function executeShow(sfWebRequest $request){
		$this->entry=$this->initEntry();
		$this->currentSection=$this->entry->getSection();

		/** @TODO: move to separate process! **/
		if ($request->hasParameter("snippetName")){
            sfConfig::set('sf_web_debug', false);
			$this->snippetName=$request->getParameter("snippetName");
			$this->setTemplate("snippet");
		}
	}

	public function executeCreate(sfWebRequest $request){
		$this->saveEntryFromRequest($request);
	}

	public function executeUpdate(sfWebRequest $request){
		$this->entry=$this->initEntry();
		$this->saveEntryFromRequest($request);
	}

	public function executePromote(sfWebRequest $request){
		$this->entry=$this->initEntry();
		$this->entry->promote();
		$this->setTemplate("XMLHttp", "global");
	}

	public function executeDemote(sfWebRequest $request){
		$this->entry=$this->initEntry();
		$this->entry->demote();
		$this->setTemplate("XMLHttp", "global");
	}
    
	public function executePosition(sfWebRequest $request){
		$this->entry=$this->initEntry();
		$this->entry->moveToPosition($request->getParameter("position"));
		$this->setTemplate("XMLHttp", "global");
	}

    public function executePositionBefore(sfWebRequest $request){
        /** @var $this->entry Entry **/
        $this->entry=$this->initEntry();

        $beforeObject=Doctrine::getTable("Entry")->find($request->getParameter("before_object_id"));
        $beforePosition=$beforeObject->getPosition();
        $newPosition=$beforePosition-1;

        if ($newPosition>0){
            $this->entry->moveToPosition($newPosition);
        }else{
            $this->entry->moveToFirst();
        }
        $this->setTemplate("XMLHttp", "global");
    }

    public function executePositionAfter(sfWebRequest $request){
        $this->entry=$this->initEntry();

        $afterObject=Doctrine::getTable("Entry")->find($request->getParameter("after_object_id"));
        $this->entry->moveToPosition($afterObject->getPosition()+1);

        $this->setTemplate("XMLHttp", "global");
    }


	public function executeDelete(sfWebRequest $request){
		$this->entry=$this->initEntry();
		$this->entry->delete();
		$this->setTemplate("XMLHttp", "global");
	}


}
